package com.crx.springbootinit.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crx.springbootinit.model.dto.comment.CommentPostIdPageDTO;
import com.crx.springbootinit.model.dto.comment.CommentsPageDTO;
import com.crx.springbootinit.model.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.crx.springbootinit.model.vo.comment.CommentChildrenVO;
import com.crx.springbootinit.model.vo.comment.CommentPageVO;
import com.crx.springbootinit.model.vo.comment.CommentsVO;
import com.crx.springbootinit.model.vo.follow.FollowPageVo;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 评论表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2024-10-21
 */
public interface CommentMapper extends BaseMapper<Comment> {
    @Select("SELECT c.*, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.post_id = #{postId} " +
            "AND c.parent_id = 0 " +
            "AND c.root_comment_id = 0 " +
            "LIMIT #{offset}, #{pageSize}")
    List<CommentsVO> getAllRoot(@Param("postId") Long postId, @Param("pageSize") int pageSize, @Param("offset") int offset);
    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId} AND parent_id = 0 AND root_comment_id = 0")
    int getAllRootTotalCount(@Param("postId") Long postId);


    @Select("select * from comment where parent_id = #{parentId} and post_id = #{momentId} LIMIT #{offset}, #{pageSize}")
    List<CommentsVO> getChildren(long parentId, long momentId,@Param("pageSize") int pageSize, @Param("offset") int offset);

    @Select("SELECT c.*, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.root_comment_id = #{rootCommentId} " +
            "AND c.post_id = #{momentId} " +
            "LIMIT #{offset}, #{pageSize}")
    List<CommentChildrenVO> getRootCommentId(long rootCommentId, long momentId, @Param("pageSize") int pageSize, @Param("offset") int offset);

    @Select("select COUNT(*) from comment where root_comment_id = #{rootCommentId} and post_id = #{momentId}")
    int getRootCommentIdTotalCount(@Param("momentId") Long momentId,@Param("rootCommentId") Long rootCommentId);


    IPage<CommentPageVO> queryCommentPage(Page<CommentPageVO> page, @Param("commentsPageDTO") CommentsPageDTO commentsPageDTO);
}
